package cn.csust.net2.manager.client.panel;


import java.util.Date;

import cn.csust.net2.manager.client.resources.Resources;
import cn.csust.net2.manager.client.ux.BeanRowEditor;
import cn.csust.net2.manager.client.ux.ExpandGrid;
import cn.csust.net2.manager.client.ux.ServiceCallback;
import cn.csust.net2.manager.shared.po.PO;
import cn.csust.net2.manager.shared.po.Parameter;
import cn.csust.net2.manager.shared.service.SetParameterService;
import cn.csust.net2.manager.shared.service.SetParameterServiceAsync;

import com.extjs.gxt.ui.client.Style.ButtonScale;
import com.extjs.gxt.ui.client.Style.IconAlign;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.store.StoreEvent;
import com.extjs.gxt.ui.client.store.Record.RecordUpdate;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

public class SetParameterPanel extends ContentPanel {
	private SetParameterServiceAsync service = GWT.create(SetParameterService.class);
	private ExpandGrid<Parameter> grid;
	private BeanRowEditor<Parameter> rowEditor;
	public SetParameterPanel() {
		setHeaderVisible(false);
		setLayout(new FitLayout());
		create();
	}
	public void create(){
		grid = new ExpandGrid<Parameter>(new Parameter(), service);
		grid.getStore().addListener(Store.Update, new StoreListener());
		grid.setColumnLines(true);
		rowEditor = new BeanRowEditor<Parameter>();
		grid.addPlugin(rowEditor);
		initTool();
		this.setWidth("100%");
		this.setHeight("80%");
		this.setHeaderVisible(false);
		this.setLayout(new FitLayout());
		this.add(grid);
		this.setBottomComponent(grid.getToolBar());
	}
	private void initTool(){
		/**
		 * 创建工具栏
		 */

		ToolBar tb = new ToolBar();
		// 增加和删除按钮，修改
		Button update = new Button("修改");
		update.setIconAlign(IconAlign.TOP);
		update.setScale(ButtonScale.LARGE);
		update.setIcon(AbstractImagePrototype.create(Resources.ICONS.details()));
		update.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				update();
			}
		});
		Button save = new Button("保存");
		save.setIconAlign(IconAlign.TOP);
		save.setScale(ButtonScale.LARGE);
		save.setIcon(AbstractImagePrototype.create(Resources.ICONS.details()));
		save.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {

				save();
			}
		});
				// 增加和删除按钮，修改
		Button back = new Button("放弃");
		back.setIconAlign(IconAlign.TOP);
		back.setScale(ButtonScale.LARGE);
		back.setIcon(AbstractImagePrototype.create(Resources.ICONS.details()));
		back.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				back();
			}
		});
		tb.add(new SeparatorToolItem());
		tb.add(update);
		tb.add(new SeparatorToolItem());
		tb.add(save);
		tb.add(new SeparatorToolItem());
		tb.add(back);
		this.setTopComponent(tb);
		
	}
	private void save(){
		grid.getStore().commitChanges();
	}
	private void back(){
		grid.getStore().rejectChanges();
	}
	private void update(){
		int count = grid.getSelectionModel().getSelectedItems().size();
		if (count == 0) {
			return;
		} else if(count >1) {
			MessageBox.alert("提示", "对不起，你每次只能选择一条数据进行修改！！！", null);
		} else {
			BeanModel model = grid.getSelectionModel().getSelectedItem();
			Date createDate = model.get("createDate");
			if(createDate!=null) {
				Date now = new Date();
				if(!now.after(createDate)) {
					Info.display("提示", "你没有权限修改这条数据");
					return;
				}
			}
			System.out.println(grid.getCurrentRow()+"正在编辑为行");
			rowEditor.startEditing(grid.getCurrentRow(), true);
			rowEditor.setAdd(false);
		}
	}
	/**
	 * 内部类 提供BEANGRID的数据处理的监听类
	 * 处理当前的grid的数组交换
	 * 
	 * @author 韩忠金
	 * 
	 */
	public class StoreListener implements Listener<StoreEvent<BeanModel>> {

		@Override
		public void handleEvent(StoreEvent<BeanModel> be) {
			
			 if (be.getType().equals(Store.Update)) {
				
				if(be.getOperation()==RecordUpdate.COMMIT){
					PO po = be.getModel().getBean();
						service.update(po,new ServiceCallback<Void>() {
		
							@Override
							public void onSuccess(Void result) {
		
								Info.display("系统信息", "Update操作成功");
		
							}
						});
				
				}
				
			}
		}
	}
}
